/*
Copyright (c) 2000, Thomas W. Christopher and George K. Thiruvathukal

Java and High Performance Computing (JHPC) Organzization
Tools of Computing LLC

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

The names Java and High-Performance Computing (JHPC) Organization,
Tools of Computing LLC, and/or the names of its contributors may not
be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This license is based on version 2 of the BSD license. For more
information on Open Source licenses, please visit
http://opensource.org.
*/

using System;


namespace info.jhpc.thread
{
	/**
	 * To be used to generate keys for FutureTables
	 * and SharedTableOfQueues. An indexed key consists of
	 * an int id and a long index. </p>The id can be specified
	 * when the indexed key is created, or it can be generated
	 * randomly. Indexed keys with the same id value
	 * may be considered names of parts of the same data
	 * structure.
	 */
	public class IndexedKey {
	    private static Random rand = new Random();
	    private static Random hasher = new Random();
	    /**
	     * The identity field. Indexed keys with the same id
	     * field are considered related, i.e. names of parts of
	     * the same data structure.
	     */
	    private int id;
	    /**
	     * The index field.
	     */
	    private long x;
	
	    private IndexedKey(long x) {
	        lock(rand) {
	            id=rand.Next(Int16.MinValue, Int16.MaxValue);        
	        }
	        this.x = x;
	    }
	
	    private IndexedKey(int id, long x) {
	        this.id = id;
	        this.x = x;
	    }
	
	    /**
	     * Factory method to create an indexed key with a random
	     * id and a specified index. The id field will not be the
	     * same as any short int.
	     *
	     * @param x The index.
	     * @return The new IndexedKey.
	     */
	    public static IndexedKey unique(long x) {
	        return new IndexedKey(x);
	    }
	
	    /**
	     * Factory method to make an indexed key with a specified
	     * id and a specified index. If the id field is a short int,
	     * it will not be the same as any id generated by unique().
	     *
	     * @param id The id field.
	     * @param x  The index.
	     * @return The new IndexedKey.
	     */
	    public static IndexedKey make(int id, long x) {
	        return new IndexedKey(id, x);
	    }
	
	    /**
	     * Get the id field.
	     *
	     * @return The id field.
	     */
	    public int getId() {
	        return id;
	    }
	
	    /**
	     * Get the index field.
	     *
	     * @return The index field.
	     */
	    public long getX() {
	        return x;
	    }
	
	    /**
	     * Create a new indexed key with the same id as this
	     * one and a specified index field.
	     *
	     * @param x The index.
	     * @return The new IndexedKey.
	     */
	    public IndexedKey at(long x) {
	        return new IndexedKey(id, x);
	    }
	
	    /**
	     * Create a new indexed key with the same id as this
	     * one and an index field differing by a specified amount
	     * from the index field of this indexed key.
	     *
	     * @param x The amount to add to the index.
	     * @return The new IndexedKey.
	     */
	    public IndexedKey add(long x) {
	        return new IndexedKey(id, this.x + x);
	    }
	
	    /**
	     * Compare two indexed key objects for equality.
	     * They are equal id their id and x fields are equal.
	     *
	     * @param o The other indexed key.
	     * @return True if they are equal, else false.
	     */
	    public bool equals(Object o) {
	        if (o is IndexedKey) {
	            IndexedKey k = (IndexedKey) o;
	            return id == k.id &&
	                    x == k.x;
	        } else
	            return false;
	    }
	
	    /**
	     * Hash an indexed key object. The hash value is
	     * computed from the id and x fields.
	     *
	     * @return the hash code.
	     */
	    public int hashCode() {
	        lock(hasher) {
	            hasher=new Random((int)(id + x));
	            hasher.Next();
	            return hasher.Next();
	        }
	    }
	
	    /**
	     * Convert the indexed key into a string.
	     *
	     * @return The string.
	     */
	    public override String ToString() {
	        return "IndexedKey(" + id + "," + x + ")";
	    }
	}
}
